<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

class admin_controller_cache extends omnitrix_controller
{
	public function index_action()
	{
		$rows = $this->_fetch_cache_list();

		$list_table = otx_html_create_table($this->_('heading_index'), 'table-list');
		$list_table->set_head_cell($this->_('column_name'));
		$list_table->set_head_cell($this->_('column_size'), array('class' => 'center', 'width' => 150));
		$list_table->set_head_cell($this->_('column_time'), array('class' => 'center', 'width' => 150));
		$list_table->set_head_cell($this->_('text_actions'), array('class' => 'center', 'width' => 150));

		if ($rows) {
			foreach ($rows as $row) {
				$list_table->add_body_row();
				$list_table->set_body_cell(otx_html_link(otx_html_strong($row['name']), array('href' => otx_router_url('view', null, null, array('cache_name' => $row['name'])))));
				$list_table->set_body_cell(otx_string_format_size($row['size']), array('class' => 'center'));
				$list_table->set_body_cell(otx_string_format_datetime($row['time']), array('class' => 'center'));
				$list_table->set_body_cell(otx_html_link($this->_('action_delete'), array('href' => otx_router_url('delete', null, null, array('cache_name' => $row['name'])), 'data-confirm' => 'confirm_delete')), array('class' => 'center'));
			};
		} else {
			$list_table->add_body_row();
			$list_table->set_body_cell($this->_('text_no_results'), array('colspan' => 4));
		}

		otx_html_add_navtab($this->_('navtab_index_title'), $this->_('navtab_index_desc', DIR_CACHE), otx_router_url('index'), true);
		otx_html_add_breadcrumb($this->_('breadcrumb_index'));
		otx_html_response_title($this->_('breadcrumb_index'));
	}

	public function delete_action()
	{
		$cache_name = otx_param_string('cache_name');
		otx_cache_delete($cache_name);

		otx_admin_flash_message($this->_('text_success_delete', $cache_name));
		otx_admin_log_action($this->_('text_success_delete', $cache_name));
		otx_router_redirect('index');
	}

	public function view_action()
	{
		$cache_name = otx_param_string('cache_name');
		$cache_content = otx_cache_fetch($cache_name);

		$view_table = otx_html_create_table($this->_('heading_view'), 'table-list');
		$view_table->set_body_cell(otx_html_tag_simple('pre', otx_html_escape(otx_string_dump($cache_content))));

		otx_html_add_navtab($this->_('navtab_index_title'), $this->_('navtab_index_desc'), otx_router_url('index'));
		otx_html_add_navtab($this->_('navtab_view_title'), $this->_('navtab_view_desc'), otx_request_uri(), true);
		otx_html_add_breadcrumb($this->_('breadcrumb_index'), otx_router_url('index'));
		otx_html_add_breadcrumb($this->_('breadcrumb_view', $cache_name));
		otx_html_response_title($this->_('breadcrumb_view', $cache_name));
	}

	protected function _fetch_cache_list()
	{
		return otx_cache_list();;
	}
}